unit TestLogReader;

interface

uses SysUtils, Classes, MuTests;

implementation

uses MuUtils, LogReader, LogData;

procedure ProcessValidFile(const FileName: string);
var
  FilePath: string;
  ErrorItems: TList;
begin
  ErrorItems := TList.Create();
  try
    FilePath := ExtractFilePath(ParamStr(0)) + 'Tests\' + FileName;
    LogReader.DoIt(FilePath, ErrorItems);
    Check(ErrorItems.Count = 0, '%d error(s) was found in a valid file', [ErrorItems.Count]);
  finally
    FreeObjectList(ErrorItems);
  end;
end;

procedure CheckOption(Index: Integer; const Id, Value: string; Lang: string = '');
var
  Option: TOption;
begin
  Pointer(Option) := OptionList[Index];
  Check(Id = Option.Id, 'Id mismatch, expected "%s", but "%s" was found', [Id, Option.Id]);
  Check(Value = Option.Value, 'Value mismatch, expected "%s", but "%s" was found', [Value, Option.Value]);
  Check(Lang = Option.Lang, 'Lang mismatch, expected "%s", but "%s" was found', [Lang, Option.Lang]);
end;

procedure CheckTypedef(Index: Integer; const Id: string);
var
  Typedef: TTypedef;
begin
  Pointer(Typedef) := TypedefList[Index];
  Check(Id = Typedef.Id, 'Id mismatch, expected "%s", but "%s" was found', [Id, Typedef.Id]);
end;

procedure CheckField(TypedefIndex, FieldIndex: Integer; const Id, TypeId: string; TypeLen: Integer; Indexed: Boolean);
var
  Typedef: TTypedef;
  Field: TField;
begin
  Pointer(Typedef) := TypedefList[TypedefIndex];
  Pointer(Field) := Typedef.FieldList[FieldIndex];
  Check(Field.Id = Id, 'Id mismatch, expected "%s", but "%s" was found', [Id, Field.Id]);
  Check(Field.VarType.Id = TypeId, 'TypeId mismatch, expected "%s", but "%s" was found', [TypeId, Field.VarType.Id]);
  Check(Field.VarType.Len = TypeLen, 'TypeLen mismatch, expected "%d", but "%d" was found', [TypeLen, Field.VarType.Len]);
  Check(Field.Indexed = Indexed, 'Indexed mismatch, expected "%s", but "%s" was found', [BoolToStr(Indexed), BoolToStr(Field.Indexed)]);
end;

procedure Test_01();
begin
  ProcessValidFile('test-01.mulog');
  Check(OptionList.Count = 4, 'OptionList.Count contains invalid value. %d expected, but %d was found', [4, OptionList.Count]);
  CheckOption(0, 'namespace', 'log', 'C++');
  CheckOption(1, 'UnitName', 'PreferansLog', 'Delphi');
  CheckOption(2, 'CreateNewDatabase', 'False', '');
  CheckOption(3, 'Server', 'localhost :3467', '');

  Check(TypedefList.Count = 2, 'TypeList.Count contains invalid value. %d expected, but %d was found', [2, TypedefList.Count]);
  CheckTypedef(0, 'HttpHeaders');
  CheckTypedef(1, 'IoOperations');

  CheckField(0, 0, 'Guid', 'Guid', 0, True);
  CheckField(0, 1, 'HttpCode', 'Integer', 4, False);
  CheckField(1, 0, 'FileName', 'String', 200, False);
end;

const
  Tests: array[1..2] of TTest = (
    (Name: 'Test_01'; Proc: Test_01;),
    (Name: ''; Proc: nil)
  );

initialization
  RegisterTests('LogReader', Tests);

end.
